import { uploadToProfile } from '@/api/modules/tool/file/file';

import { resetFileName } from '@/utils/file/file';

// or, from each individual module
import ImageUploader from 'quill-image-uploader';

/* quill通用菜单栏 */
export const quillToolbar = [
  ['bold', 'italic', 'underline', 'strike'], // toggled buttons
  ['blockquote', 'code-block'],
  ['link', 'image', 'video', 'formula'],

  [{ header: 1 }, { header: 2 }], // custom button values
  [{ list: 'ordered' }, { list: 'bullet' }, { list: 'check' }],
  [{ script: 'sub' }, { script: 'super' }], // superscript/subscript
  [{ indent: '-1' }, { indent: '+1' }], // outdent/indent
  [{ header: [1, 2, 3, 4, 5, 6, false] }],

  [{ color: [] }, { background: [] }], // dropdown with defaults from theme
  [{ font: [] }],
  [{ align: [] }],

  ['clean'], // remove formatting button
];

/**
 * @description:
 * @param {string} config.url 上传文件的路径
 * @param {string} config.root 文件根路径
 *
 */

export const getQuillModule = () => {
  return [
    {
      name: 'imageUploader',
      module: ImageUploader,
      options: {
        upload: (file: File) => {
          return new Promise(async resolve => {
            const modifiedFile = resetFileName(file);
            const data = await uploadToProfile({
              files: [
                {
                  file: modifiedFile,
                  path: modifiedFile.name,
                },
              ],
              url: 'common/uploads',
            });
            /* 调用成功的回调路径 */
            // @ts-ignore
            let imagePath = data.urls;
            resolve(imagePath);
          });
        },
      },
    },
  ];
};
